package com.lzy.controller;

import com.lzy.base.BaseInfoProperties;
import com.lzy.enums.YesOrNo;
import com.lzy.grace.result.GraceJSONResult;
import com.lzy.grace.result.ResponseStatusEnum;
import com.lzy.pojo.Users;
import com.lzy.pojo.bo.ModifyUserBO;
import com.lzy.pojo.vo.ContactsVO;
import com.lzy.pojo.vo.UsersVO;
import com.lzy.service.FriendShipService;
import com.lzy.service.UsersService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("friendship")
@Slf4j
public class FriendShipController extends BaseInfoProperties {

    @Autowired
    private FriendShipService friendShipService;

    @PostMapping("getFriendship")
    public GraceJSONResult getFriendship(String friendId,HttpServletRequest request){

        String myId = request.getHeader(HEADER_USER_ID);

        return GraceJSONResult.ok(friendShipService.getFriendship(myId,friendId));
    }

    @PostMapping("queryMyFriends")
    public GraceJSONResult queryMyFriends(HttpServletRequest request){

        String myId = request.getHeader(HEADER_USER_ID);

        List<ContactsVO> contactsVOList = friendShipService.queryMyFriends(myId,false);
        return GraceJSONResult.ok(contactsVOList);
    }

    @PostMapping("queryMyBlackList")
    public GraceJSONResult queryMyBlackList(HttpServletRequest request){
        String myId = request.getHeader(HEADER_USER_ID);
        return GraceJSONResult.ok(friendShipService.queryMyFriends(myId,true));
    }

    @PostMapping("updateFriendRemark")
    public GraceJSONResult updateFriendRemark(HttpServletRequest request,
                                              String friendId,
                                              String friendRemark){

        if(StringUtils.isBlank(friendRemark) || StringUtils.isBlank(friendId)){
            return GraceJSONResult.error();
        }

        String myId = request.getHeader(HEADER_USER_ID);
        friendShipService.updateFriendRemark(myId,friendId,friendRemark);
        return GraceJSONResult.ok(friendShipService.queryMyFriends(myId,false));
    }

    @PostMapping("moveOutBlack")
    public GraceJSONResult moveOutBlack(HttpServletRequest request,
                                        String friendId){
        String myId = request.getHeader(HEADER_USER_ID);
            friendShipService.updateBlackList(myId,friendId,YesOrNo.NO);
        return GraceJSONResult.ok(friendShipService.getFriendship(myId,friendId));
    }

    @PostMapping("tobeBlack")
    public GraceJSONResult tobeBlack(HttpServletRequest request,
                                     String friendId){
        String myId = request.getHeader(HEADER_USER_ID);
        friendShipService.updateBlackList(myId,friendId,YesOrNo.YES);
        return GraceJSONResult.ok(friendShipService.getFriendship(myId,friendId));
    }


    @PostMapping("delete")
    public GraceJSONResult delete(HttpServletRequest request,
                                     String friendId){
        String myId = request.getHeader(HEADER_USER_ID);
        friendShipService.delete(myId,friendId);
        return GraceJSONResult.ok();
    }

    /**
     * 判断是否黑名单
     */
    @GetMapping("isBlack")
    public GraceJSONResult isBlack(String friendId1st,String friendId2nd){

        // 需要进行两次查询，A拉黑B，B拉黑A，或者AB拉黑
        // 只需要符合其中的一个条件，就表示双方的消息不可到达
        return GraceJSONResult.ok(
                friendShipService
                        .isBlackEachOther(friendId1st,friendId2nd));
    }
}
